package com.event.management.frontend.autoConfig;

import com.event.management.frontend.config.ArkProperties;
import com.volcengine.ark.runtime.service.ArkService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Slf4j
@Configuration
@EnableConfigurationProperties(ArkProperties.class)
public class ArkAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ConnectionPool arkConnectionPool(ArkProperties properties) {
        return new ConnectionPool(properties.getMaxIdleConnections(), 
                properties.getKeepAliveDuration(), TimeUnit.SECONDS);
    }
    
    @Bean
    @ConditionalOnMissingBean
    public Dispatcher arkDispatcher() {
        return new Dispatcher();
    }
    
    @Bean
    @ConditionalOnMissingBean
    public ArkService arkService(ArkProperties properties, 
                                 ConnectionPool connectionPool, 
                                 Dispatcher dispatcher) {
        log.info("正在初始化火山引擎Ark服务...");
        return ArkService.builder()
                .dispatcher(dispatcher)
                .connectionPool(connectionPool)
                .baseUrl(properties.getBaseUrl())
                .apiKey(properties.getApiKey())
                .build();
    }
} 